/** 
 * Project Name:sys-portal-Extjs 
 * File Name:MainCtrl.js 
 * Package Name: 
 * Author : 吴Yuan|小森森
 * Date:2018年4月16日下午7:35:43 
 * Copyright (c) 2018, 15909910367@163.com All Rights Reserved. 
 * 
 */
Ext.define('Jason.view.protal.module.systemManager.right.rights.MainCtrl', {
    extend: 'Wys.basic.BaseCtrl',
    alias: 'controller.right_rights_MainViewCtrl',
    formItems: function () {
        return [{
            xtype: 'fieldset',
            title: '基本信息',
            itemId: 'rightBasicForm',
            collapsible: false,
            defaultType: 'textfield',
            defaults: {
                width: '100%',
                labelWidth: 80
            },
            items: [
                { fieldLabel: 'id', name: 'id', xtype: 'textfield', flex: 1, anchor: '100%', hidden: true, value: '' },
                { fieldLabel: '权限名称', name: 'rightname', xtype: 'textfield', allowBlank: false, afterLabelTextTpl: cfg.required },
                {
                    xtype: 'comboTree',
                    valueField: 'modularcode',
                    displayField: 'showname',
                    rootVisible: false,
                    store: Ext.create('Ext.data.TreeStore', {
                        fields: [
                            { name: 'id', mapping: 'modularcode' },
                            { name: 'text', mapping: 'showname' }
                        ],
                        autoLoad: true,
                        proxy: {
                            type: 'ajax',
                            url:  reqUrl.sys.sources.menus.modularTree,
                            rootProperty: function (data) {
                                return data;
                            }
                        }
                    }),
                    listeners: {
                    	dirtychange:function(picker, dirty, eOpts){
                    	},
                        select: function (picker, record, eOpts) {
                            component = picker.up('#rightBasicForm').down('#selectionRightByResources');
                            if (record.data.leaf) {
                                var cfg_ = {
                                    url: reqUrl.sys.comboData.searchRight + "?type=ModularRight&keycode=" + record.data.modularright,
                                    method: 'GET',
                                    callback: function (resp) {
                                        var rightObj = resp.data,
                                            items_field = new Array();
                                        Ext.Array.each(rightObj, function (items) {
                                            var item = {};
                                            item.boxLabel = items.key;
                                            item.inputValue = items.keycode;
                                            item.name = "rightcodes";
                                            item.margin = '0 5 0 0';
                                            items_field.push(item);
                                        });
                                        component.removeAll();
                                        component.add(items_field);
                                        component.show();
                                    }
                                };
                                EUtil.RS(cfg_);
                            } else {
                                component.hide();
                                EUtil.toastWarn('请选择叶子节点进行授权');
                            }
                        }
                    },
                    fieldLabel: '资源',
                    name: 'sourcescode',
                    allowBlank: false,
                    afterLabelTextTpl: cfg.required
                },
                {
                    xtype: 'fieldset',
                    columnWidth: 0.5,
                    title: '选择拥有的操作',
                    itemId: 'selectionRightByResources',
                    defaultType: 'checkboxfield',
                    hidden: true,
                    layout: { type: 'table', columns: 9 }
                },
                { fieldLabel: '备注', name: 'remarks', flex: 1, xtype: 'textareafield', grow: true, anchor: '100%' }
            ]
        }];
    },

    onNewButtonClick: function (btn) {
        var me = this;
        Ext.create('Wys.form.FormWindow', {
            width: 500,
            title: '权限管理',
            model: '',
            config: {
                operatetype: 'new',
                model: 'Jason.model.protal.module.systemManager.right.rights.RightModel',
                gridPanel: btn.up('#rightsView'),
                items: me.formItems()
            }
        }).show();
    },
    onEditButtonClick: function (grid, record) {
        var me = this;
        var grid = me.getView().down('baseView');
        var records = grid.getSelection();
        if (records.length === 0 || records.length > 1) {
            EUtil.toastWarn('请选择要编辑的数据。<br /><span style="color:red">注：一次只能选择一条数据进行编辑</span>');
            return;
        } else {
            record = records[0];
        }
        Ext.create('Wys.form.FormWindow', {
            width: 500,
            title: ' 『' + record.get('rightname') + '』权限信息',
            config: {
                model: 'Jason.model.protal.module.systemManager.right.rights.RightModel',
                operatetype: 'edit',
                gridPanel: grid,
                items: me.formItems(),
                formData: record
            }
        }).show();
    },
    onDeleteButtonClick: function (btn) {
        var grid = this.getView().down('#rightsView'),
            gridStore = grid.getStore(),
            selectGridData = grid.getSelection(),
            removePK = [],
            removeName = [];
        if (selectGridData.length === 0) {
            EUtil.toastWarn('请选择要操作的数据');
            return;
        }
        Ext.Array.each(selectGridData, function (item) {
            removeName.push(item.data.rightname);
            removePK.push(item.data.id);
        });
        EUtil.showMsg({
            option: 3,
            title: '温馨提示',
            message: '您确定删除<span style="color:red;">『' + removeName.join() + '』</span>吗？',
            callback: function (btn) {
                if (btn === 'ok') {
                    gridStore.remove(selectGridData);
                    gridStore.sync({
                        callback: function () {
                            gridStore.reload();
                        }
                    });
                }
            }
        });
    },
    onChangeUserState: function (btn) {
        var grid = this.getView().down('#rightsView'),
            gridStore = grid.getStore(),
            selectGridData = grid.getSelectValues(),
            removePK = [],
            removeName = [];
        if (selectGridData.length === 0) {
            EUtil.toastWarn('请选择要操作的数据');
            return;
        }
    },

    refreshLeftMenuTree: function (btn) {
        var treeStore = btn.up('treepanel').getStore();
        treeStore.load();
    },
    onTreeNodeClick: function (node, e) {
        var self = this;
        self.getView().down('#rightsView').load({
            sourcescode: e.get('modularcode')
        })
    }
});